<?php

namespace Common\Model;

use Think\Model;

/**
 * 回复模型
 */
class AnsModel extends Model {

    const NORMAL = 0;   // 回复状态：正常
    const DELTEL = -1;  // 回复状态：删除

    /**
     * 向回复表中新增一条数据
     * @param $data
     * @return mixed
     */
    public static function addData($data){
        return M('Ans')->add($data);
    }

    /**
     * 新建回复
     * @param $tie
     * @param $data
     * @return bool|mixed
     */
    public static function createAns($tie, $data){
        $model = M();
        $model->startTrans();
        // 先添加回复数据
        $aid = self::addData($data);
        if(!$aid){
            $model->rollback();
            return false;
        }
        // 再更新帖子的更新时间和回复数
        if(!TieModel::reply($data['tid'])){
            $model->rollback();
            return false;
        }
        // 再更新团体的回复数
        if(!BbsCountModel::update($tie['bid'], 'hf')){
            $model->rollback();
            return false;
        }
        // 对帖子作者的消息通知
        $msg = [];
        if($data['uid'] != $tie['uid']){
            $msg[] = [
                'sid' => $data['uid'],
                'gid' => $tie['uid'],
                'content' => '在问答「<a href="//doc.wooo.wang/t/'.$tie['id'].'.html">'.$tie['title'].'</a>」中回复你：<a href="//doc.wooo.wang/t/'.$tie['id'].'.html">'.strip_tags($data['html']).'</a>',
                'create' => time()
            ];
        }
        // 最后添加通知记录【可以添加队列】
        if($msg){
            $msgResult = MsgModel::addDatas($msg);
            if(!$msgResult){
                $model->rollback();
                return false;
            }
        }
        $model->commit();
        return $aid;
    }

    /**
     * 更新回复的数据信息
     * @param $aid
     * @param $data
     * @return bool
     */
    public static function updateAns($aid, $data){
        return M('Ans')->where(array('id' => $aid))->save($data);
    }

    /**
     * 获取帖子的回复列表数据
     * @param $tid
     * @param $p
     * @param $pcount
     * @return array|bool
     */
    public static function getAnsList($tid, $p, $pcount){
        $count = M('Ans')->where(['tid' => $tid, 'status' => self::NORMAL])->count();
        $list = $count ? M()->table(['wo_ans' => 'a', 'wo_user' => 'u'])->where(['a.tid' => $tid, 'a.status' => self::NORMAL])->where('a.uid = u.id')->field('a.id,a.html,a.create,a.uid,u.nick,u.avatar,u.desc')->page($p.','.$pcount)->select() : array();
        return [
            'count' => $count,
            'list' => $list
        ];
    }

    /**
     * 获取回复的回复列表数据
     * @param $tid
     * @param $fid
     * @param $p
     * @param $pcount
     * @return array|bool
     */
    public static function getAnsAnsList($tid, $fid, $p, $pcount){
        $count = M('Ans')->where(array('tid' => $tid, 'fid' => $fid, 'status' => self::NORMAL))->count();
        $list = $count ? M()->table(array('wo_ans' => 'a', 'wo_user' => 'u'))->where(array('a.tid' => $tid, 'a.fid' => $fid, 'a.status' => self::NORMAL))->where('a.uid = u.id')->field('a.id,a.html,a.create,a.uid,u.nick')->page($p.','.$pcount)->select() : array();
        $res = array('count' => $count, 'list' => $list);
        return $res;
    }

    /**
     * 补充回复的内容
     * @param $aid
     * @param $html
     * @return mixed
     */
    public static function publishPatch($aid, $html){
        $html = '<p class="faq-update-msg">'.date('Y-m-d H:i:s',time()).' &nbsp;<strong>Update：</strong></p>'.filterXss($html);
        $data = array(
            'html' => array('exp', 'concat(`html`, "'.addslashes($html).'")')
        );
        return M('Ans')->where(array('id' => $aid))->save($data);
    }

    /**
     * 删除回复
     * @param $tid
     * @param $aid
     * @return bool
     */
    public static function deleteAns($tid, $aid){
        return M('Ans')->where(array('id' => $aid, 'tid' => $tid))->setField('status', self::DELTEL);
    }

}